import pygame


class Paddle:
    ''' 球拍类 '''

    def __init__(self, game):
        self.screen = game.screen  # 获取游戏屏幕对象
        self.screen_rect = game.screen.get_rect()  # 获取屏幕的矩形区域
        self.settings = game.settings  # 获取游戏设置
        self.color = game.settings.paddle_color  # 设置球拍颜色

        # 创建球拍的矩形对象
        self.rect = pygame.Rect(0, 0, self.settings.paddle_width, self.settings.paddle_height)

        # 标志位，防止球拍瞬间移动
        self.moving_up = False
        self.moving_down = False

        self.y = float(self.rect.y)  # 用浮点数表示球拍的纵坐标

    def draw_paddle(self):
        ''' 绘制球拍到屏幕 '''
        pygame.draw.rect(self.screen, self.color, self.rect)

    def update(self):
        ''' 根据按键移动球拍 '''
        if self.moving_up and self.rect.top >= 0:  # 向上移动，确保不超出屏幕
            self.y -= self.settings.paddle_speed
        if self.moving_down and self.rect.bottom <= self.settings.screen_height:  # 向下移动，确保不超出屏幕
            self.y += self.settings.paddle_speed

        self.rect.y = self.y  # 更新球拍的y坐标


class PaddleLeft(Paddle):
    ''' 左侧球拍类 '''

    def __init__(self, game):
        super().__init__(game)
        self.reset_paddle()

    def reset_paddle(self):
        ''' 将球拍放置在正确位置，如果进球则重置球拍位置 '''
        # 将球拍放置在屏幕左侧中部
        self.rect.midleft = self.screen_rect.midleft
        self.rect.x += 50  # 右移50个像素
        self.y = float(self.rect.y)  # 更新浮点数y坐标


class PaddleRight(Paddle):
    ''' 右侧球拍类 '''

    def __init__(self, game):
        super().__init__(game)
        self.reset_paddle()

    def reset_paddle(self):
        ''' 将球拍放置在正确位置，如果进球则重置球拍位置 '''
        # 将球拍放置在屏幕右侧中部
        self.rect.midright = self.screen_rect.midright
        self.rect.x -= 50  # 左移50个像素
        self.y = float(self.rect.y)  # 更新浮点数y坐标
